/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import akka.actor.ActorRef;
import com.cxhd.lis.DataCache;
import com.cxhd.lis.ctrl.actor.ActorManager;
import com.cxhd.lis.model.newmodel.LisMatchinfo;
import com.cxhd.lis.model.newmodel.LisPatient;
import com.cxhd.lis.model.newmodel.LisPatientInfo;
import com.cxhd.lis.model.newmodel.LisSamplecollection;
import com.cxhd.lis.model.newmodel.LisSqd;
import com.cxhd.lis.model.newmodel.LisUser;
import com.cxhd.lis.model.newmodel.lisTestResult;
import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.Tool;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.tx.TxConfig;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/host")
public class HostCtrl extends Controller {
     private static final Log logs = Log.getLog(HostCtrl.class);
    private LisSamplecollection info = new LisSamplecollection();
    private lisTestResult listest = new lisTestResult();
    private Gson gson = new Gson();

    public void index() {
        render("/view/host_1.jsp");
    }

    public void getList() {
        String sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK ";
        String where = "where ";

        if (!StringUtils.isBlank(getPara("status"))) {
            if (getPara("status").equalsIgnoreCase("1")) {
                where += "b.RESULTSTATUS =1 and";
            } else if (getPara("status").equalsIgnoreCase("2")) {
                where += "b.RESULTSTATUS =2 and";
            } else if (getPara("status").equalsIgnoreCase("3")) {
                where += "b.RESULTSTATUS =3 and";
            } else if (getPara("status").equalsIgnoreCase("5")) {
                where += "b.RESULTSTATUS is null and";
            }
        }

        if (!StringUtils.isBlank(getPara("device"))) {
            if (!getPara("device").equalsIgnoreCase("all")) {
                where += " a.DEVICEID='" + getPara("device") + "' AND ";
            }
        }
        where += "  a.MEASURETIME BETWEEN '"+ getPara("sample_date")+" 00:00:00' and '"+ getPara("sample_date")+" 23:59:00'  and PARENT_UUID IS NULL and a.DATA1 IS NULL group by a.UUID order by a.SAMPLENO ";

        sql += where;
//        if (getPara("sample_date") != null) {
//            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
//        }

//                where date(a.MEASURETIME) = curdate()
        System.out.println(sql);
        renderJson(Db.find(sql));
    }

    public void getShenHe() {
        String sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where b.RESULTSTATUS >=2 and date(a.MEASURETIME)=CURDATE()"
                + "and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO  ";
        if (getPara("sample_date") != null) {
            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
        }
        renderJson(Db.find(sql));
    }

    public void getNoShenHe() {
        String sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where b.RESULTSTATUS=1 and date(a.MEASURETIME)=CURDATE() "
                + "and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO  ";
        if (getPara("sample_date") != null) {
            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
        }
        renderJson(Db.find(sql));
    }

    public void getDaYin() {
        String sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where b.PRINTTIME is not null and date(a.MEASURETIME)=CURDATE()"
                + "  and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO ";
        if (getPara("sample_date") != null) {
            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
        }
        renderJson(Db.find(sql));
    }

    public void getNoResult() {
        String sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where b.RESULTSTATUS is null and date(a.MEASURETIME)=CURDATE() "
                + " and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO ";
        if (getPara("sample_date") != null) {
            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
        }
        renderJson(Db.find(sql));
    }

    public void changeType() {
        String sql = "";
        if (!getPara("type").equalsIgnoreCase("all")) {
            sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                    + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                    + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where a.DEVICEID='" + getPara("type") + "' AND date(a.MEASURETIME)=CURDATE() "
                    + "and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO ";
        } else {
            sql = "select DISTINCT a.UUID,a.SAMPLENO,a.MEASURETIME,a.DEVICEID,b.PK,b.CHECKOPERATOR,b.RESULTSTATUS,c.PATIENTID,c.PATIENTNAME,c.SEX,"
                    + "b.RECEIVER,c.AGE,b.SECTION,b.BED_NO,b.STAYHOSPITALMODE,b.PRINTTIME,b.SJYS,b.JYYS,b.EXAMINAIM from lis_testresult a "
                    + "LEFT JOIN lis_samplecollection b on a.UUID=b.UUID LEFT  JOIN lis_patient c on b.INFOPK=c.PK where date(a.MEASURETIME) = curdate() "
                    + "and PARENT_UUID IS NULL  group by a.UUID order by a.SAMPLENO ";
        }
        if (getPara("sample_date") != null) {
            sql = sql.replace("curdate()", "'" + getPara("sample_date") + "'");
        }
        renderJson(Db.find(sql));
    }

    public void getType() {
        renderJson(Db.use("mysql").find("SELECT DEVICEMODEL,DEVICENAME from lis_deviceset"));
    }

    public void getKsmc() {
        renderJson(Db.use("mysql").find("select KSMC from lis_ksdm"));
    }

    public void getUser() {
        renderJson(Db.use("mysql").find("select name from lis_user where position <> 'admin'"));
    }

    public void getSample() {
        renderJson(Db.use("mysql").find("select SAMPLEDESCRIBE from lis_sampletype"));
    }

    public void getDeviceSample() {
        renderJson(Db.findFirst("select * from lis_deviceset where DEVICEID='" + getPara("DEVICEID") + "'"));
    }

    public void getCharginitem() {
        renderJson(Db.use("mysql").find("select ITEMNAME from lis_charginitem"));
    }

    public void getResult() {
        //old version
//        String sql = "select a.PK,a.RESULTFLAG,a.TESTRESULT,a.TESTID,a.ITEMPK,a.DEVICEID,a.UUID,b.CHINESENAME,a.UNIT,a.TESTSTATUS,a.SAMPLETYPE,a.FEE,"
//                + "a.MEASURETIME,c.MREFLO0,c.MREFHI0,c.Decimalplace from lis_testresult a LEFT JOIN lis_testdescribe b on a.ITEMPK=b.PK "
//                + "LEFT JOIN lis_referencevalue c ON b.TESTID=c.TESTID  "
//                + "where a.UUID='" + getPara("UUID") + "'";

//new version
        String sql = "select a.PK,a.RESULTFLAG,a.TESTRESULT,a.TESTID,a.ITEMPK,a.DEVICEID,a.UUID,b.CHINESENAME,c.UNIT,a.TESTSTATUS,a.SAMPLETYPE,a.FEE,"
                + "a.MEASURETIME,c.MREFLO0,c.MREFHI0,c.FREFLO0,c.FREFHI0,c.Decimalplace,c.DATA1 AS `index`,c.DATA3 AS ranges from lis_testresult a LEFT JOIN lis_testdescribe b on a.ITEMPK=b.PK "
                + "LEFT JOIN pp_device_test c ON a.TESTID=c.TESTID and a.DEVICEID=c.DEVICEID  "
                + "where a.UUID='" + getPara("UUID") + "'";
        //+ "where (a.UUID='" + getPara("UUID") + "' or PARENT_UUID='" + getPara("UUID") + "')";
        System.out.println(sql);
        renderJson(Db.use("mysql").find(sql));
    }

    public void getBrInfoUUID() {
        String sql = "select a.*,b.PK as samplepk,b.BARCODE,b.SAMPLE,b.CYSJ,b.STAYHOSPITALMODE,b.SECTION,b.DIAGNOSTIC,b.EXAMINAIMCODE,b.EXAMINAIM,BED_NO,"
                + "b.CUSTOM1,b.CUSTOM2,b.UUID,b.CHECKOPERATOR,b.CHECKTIME,b.PRINTTIME,b.SJYS,b.JYYS,b.SELF_TYPE,b.ROOM_TEMPERATURE,b.REAGENT "
                + "from lis_patient a LEFT JOIN lis_samplecollection b on a.PK=b.INFOPK where b.UUID='" + getPara("UUID") + "'";
        Record d = Db.findFirst(sql);
        if (d != null) {
            renderJson(d);
        } else {
            renderText("false");
        }
    }

    public void getPaixu() {
        String sql = "select TESTPK,DATA1 from pp_device_test a LEFT JOIN lis_deviceset b on a.DEVICEPK=b.PK where b.DEVICEMODEL='" + getPara("devi") + "'";
        renderJson(Db.find(sql));
    }

    public void getBrInfo() {
        //从HIS获取数据
        List<Record> d = null;

        try {

            if (DataCache.getIntance().getConfig().get("hisDbFlag").equalsIgnoreCase("true")) {
                //System.out.println("select b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no where a.Patient_code like '%1610006681' and DateDiff(dd,b.Check_date,getdate())<="+day);
                //d = Db.use("hisdb").findFirst("select office as SECTION, Patient_Code as PATIENTID,Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_sex as AGE from Ut_Lis_patient_info where Patient_code like '%" + getPara("PATIENTID") + "' order by Require_Date desc ");
                //不带医生和诊断 d = Db.use("hisdb").find("select b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no where a.Patient_code like '%" + getPara("PATIENTID") + "' and DateDiff(dd,b.Check_date,getdate())<="+day);
                d = Db.use("hisdb").find(DataCache.getIntance().getSelectHisSql(getPara("PATIENTID")));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (d == null) {
            String sql = "select * from lis_patient  where PATIENTID='" + getPara("PATIENTID") + "'";
            d = Db.find(sql);
        }
        if (d != null) {
            renderJson(d);
        } else {
            renderText("false");
        }
    }

    public void getBrInfoTmh() {

        Record d = null;
        try {
            if (DataCache.getIntance().getConfig().get("hisDbFlag").equalsIgnoreCase("true")) {
                d = Db.use("hisdb").findFirst("select b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no where Sample_No like '%" + getPara("BARCODE") + "' order by Require_Date desc ");
                System.out.println(d.toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (d == null) {
            String sql = "select a.*,b.PK as samplepk,b.BARCODE,b.SAMPLE,b.CYSJ,b.STAYHOSPITALMODE,b.SECTION,b.DIAGNOSTIC,b.EXAMINAIMCODE,b.EXAMINAIM,BED_NO,"
                    + "b.UUID,b.CHECKOPERATOR,b.CHECKTIME,b.PRINTTIME,b.YBLB,b.SJYS,b.JYYS "
                    + "from lis_patient a LEFT JOIN lis_samplecollection b on a.PK=b.INFOPK where b.BARCODE='" + getPara("BARCODE") + "'";
            d = Db.findFirst(sql);
        }
        if (d != null) {
            renderJson(d);
        } else {
            renderText("false");
        }
    }

    public void xmquery() {

        //String sql = "select TESTID,CHINESENAME,UNIT,PY,PK from lis_testdescribe where TESTID LIKE '%" + getPara("py") + "%' LIMIT " + getPara("matchCount");
        String sql = "select a.TESTID,b.CHINESENAME,a.UNIT,b.PK,a.MREFLO0,a.MREFHI0,a.DATA2 from pp_device_test a LEFT JOIN lis_testdescribe b on a.TESTID=b.TESTID where a.TESTID LIKE '%" + getPara("py") + "%' AND a.DEVICEID ='" + getPara("deviceid") + "'  LIMIT " + getPara("matchCount");
        renderJson(Db.use("mysql").find(sql));
    }

    public void queryItemValue() {
        String sql = "select * from lis_reference where TESTID ='" + getPara("TESTID") + "' LIMIT " + getPara("matchCount");
        renderJson(Db.use("mysql").find(sql));
    }

    public void getMaxPK() {
        renderJson(Db.use("mysql").findFirst("select max(PK)+1 PK from lis_testresult"));
    }

    public void getZT() {
        renderJson(Db.use("mysql").find("select ITEMID,ITEMNAME from lis_charginitem"));
    }

    public void getZTMX() {
        //String sql = "select a.TESTID,b.CHINESENAME,a.UNIT,b.PK,a.MREFLO0,a.MREFHI0,a.DATA2 from pp_device_test a LEFT JOIN lis_testdescribe b on a.TESTID=b.TESTID where a.TESTID LIKE '%" + getPara("py") + "%' AND a.DEVICEID ='" + getPara("deviceid") + "'  LIMIT " + getPara("matchCount");

//        String sql = "select b.PK,a.TESTID,b.CHINESENAME,b.UNIT,c.MREFHI0,c.MREFLO0  from lis_charginitem_test a LEFT JOIN lis_testdescribe b on a.TESTPK=b.PK "
//                + " LEFT JOIN lis_referencevalue c on a.TESTID=c.TESTID where  a.ITEMID=" + getPara("id");
        String sql = "select a.TESTID,c.CHINESENAME,a.UNIT,c.PK,a.MREFLO0,a.MREFHI0,a.DATA2 as TESTRESULT from pp_device_test a LEFT JOIN lis_charginitem_test b on a.TESTID=b.TESTID LEFT JOIN lis_testdescribe c on c.TESTID=b.TESTID   where  b.ITEMID=" + getPara("id") + " and a.DEVICEID='" + getPara("deviceid") + "'";
        renderJson(Db.use("mysql").find(sql));
    }

    public void checkMM() {
        String userId = getSessionAttr("userId");
        LisUser us = new LisUser().checkLogin(userId, getPara("mm"));
        if (us != null) {
            renderText("true");
        } else {
            renderText("false");
        }
    }

    @Before(Tx.class)
    public void piLiangSH() {
        boolean flag = true;
        String returnResult = "true";
        try {
            String name = getSessionAttr("name");
            String pks[] = getPara("pks").split(",");
            System.out.println(DataCache.getIntance().getConfig().get("sh_check"));
            if (DataCache.getIntance().getConfig().get("sh_check") != null && DataCache.getIntance().getConfig().get("sh_check").equalsIgnoreCase("true")) {
                for (String pk : pks) {

                    Record info = Db.findFirst("select * from lis_samplecollection where JYYS='" + name + "'and PK=" + pk);
                    if (info != null && info.get("JYYS") != null) {
                        returnResult = info.get("SAMPLENO") + " 检验医生为【" + info.get("JYYS") + "】与审核医生相同,不能审核!";
                        flag = false;
                        break;
                    }
                }
            }
            if (flag) {
                info.updateResult(getPara("pks"), name);
                //上传到融合平台

                for (String pk : pks) {
                    
                    ActorManager.getIntance().getActor("SendDataActor").tell(pk, ActorRef.noSender());
                    
                }
            }
//            Gson gson = new Gson();
//            List<Map<String, String>> list = gson.fromJson(getPara("ybsz"), new TypeToken<List<Map<String, String>>>() {
//            }.getType());
//            String name = getSessionAttr("name");
//            for (Map<String, String> map : list) {
//                new LisMatchinfo().updateResult(map, name);
//            }
            renderText(returnResult);
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }
    public void cancelSH(){
     
        String name=getSessionAttr("name");
        System.out.println("select * from lis_samplecollection where CHECKOPERATOR='"+name+"' and PK = "+getPara("sample_pk"));
        Record info = Db.findFirst("select * from lis_samplecollection where CHECKOPERATOR='"+name+"' and PK = "+getPara("sample_pk"));
        if(info!=null&&info.get("CHECKOPERATOR")!=null){
            Db.update("update lis_samplecollection set CHECKOPERATOR=null ,RESULTSTATUS=1 where PK = "+getPara("sample_pk"));
            renderText("true");
        }else{
            renderText("与审核人不符合,不能取消审核");
        }
        
    }

//    public void saveMatch() {
//        String req = getSessionAttr("name");
//        LisMatchinfo info = new LisMatchinfo();
//        try {
//            info.saveMatch(this, req);
//            renderText("true");
//        } catch (Exception e) {
//            e.printStackTrace();
//            renderText("false");
//        }
//        
//    }
    @Before(Tx.class)
    public void deleteInfo() {
        try {
            String uuids[] = getPara("UUID").split(",");
            for (String uuid : uuids) {
                Db.update("delete from lis_samplecollection where UUID='" + uuid + "'");
                Db.update("delete from lis_testresult where UUID='" + uuid + "'");
            }
//            info.deleteInfo(this);
//            listest.deleteResult(getPara("UUID"));

            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void deleteXM() {
        try {
            new lisTestResult().deletePK(getPara("pk"));
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    @Before(Tx.class)
    public void saveResult() {
        try {
            long tie = new Date().getTime();
            String req = getSessionAttr("name");
            String sampleNo = getPara("sampleNo");
            String uuid = getPara("UUID");
            String sgstatus = getPara("sgstatus");
            String sgDevice = getPara("sgDevice");
            String datetime = DateUtil.getDateEN();
            System.out.println("sgstatus:" + sgstatus);
            List<Map<String, String>> result = gson.fromJson(getPara("results"), new TypeToken<List<Map<String, String>>>() {
            }.getType());

            if (sgstatus != null && sgstatus.equalsIgnoreCase("true")) {
                sampleNo = listest.getSampleNO(sgDevice);
                uuid = Tool.string2MD5(datetime.trim() + sampleNo + sgDevice);
                System.out.println("sampleNo:" + sampleNo + "  " + uuid);
            }
            String infoPk = info.saveSampleCollection(this, req, sampleNo, uuid, null);
            System.out.println("sample:" + (new Date().getTime() - tie));
            listest.deleteResult(uuid);
            System.out.println("delete:" + (new Date().getTime() - tie));
            for (Map<String, String> map : result) {
              
                listest.addResult(map, sampleNo, uuid, datetime, sgDevice);
            }
            System.out.println("保存结果:" + (new Date().getTime() - tie));
            renderJson(Db.findFirst("select a.PK,b.PATIENTID from lis_samplecollection a LEFT JOIN lis_patient b on a.INFOPK=b.PK where a.UUID='" + uuid + "'"));
            System.out.println("返回结果:" + (new Date().getTime() - tie));
        } catch (Exception e) {
           logs.error("exception",e);
            e.printStackTrace();
            renderText("false");
        }

    }

    public void saveMany() {

        try {
            String req = getSessionAttr("name");
            List<Map<String, String>> uuidList = gson.fromJson(getPara("uuids"), new TypeToken<List<Map<String, String>>>() {
            }.getType());
            info.saveSampleCollection(this, req, null, null, uuidList);
            renderText("true");
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }

    }

    //保存多个手工项目
//    @Before(Tx.class)
    public void saveSg() {
        String datetime = DateUtil.getDateEN();
        String uuid = "";
        String sampleNo = "";
        String sgDevice = getPara("sgDevice");
        int count = getParaToInt("count", 1);
        try {
            List<Map<String, String>> result = gson.fromJson(getPara("data"), new TypeToken<List<Map<String, String>>>() {
            }.getType());
            for (int i = 0; i < count; i++) {
                sampleNo = listest.getSampleNO(sgDevice);
                uuid = Tool.string2MD5(datetime.trim() + sampleNo + sgDevice);

                System.out.println("sampleNo:" + sampleNo + "  " + uuid);
                for (Map<String, String> map : result) {
                    System.out.println("_____________________" + map + "---------------");

                    new lisTestResult().addResult(map, sampleNo, uuid, datetime,sgDevice);

                }
            }
            renderText("true");
        } catch (Exception ex) {
            Logger.getLogger(HostCtrl.class.getName()).log(Level.SEVERE, null, ex);
            renderText("false");
        }
    }

//    public void updateBrXX() {
//        try {
//            new LisPatient().updateXX(this);
//            new LisSamplecollection().updateXX(this);
//            new LisMatchinfo().updateXX(this);
//            renderText("true");
//        } catch (Exception e) {
//            e.printStackTrace();
//            renderText("false");
//        }
//    }
    public void printReport() {
        try {
            new LisSamplecollection().updateDY(this);
            System.out.println(getPara("device"));
            Record re = Db.findFirst("select * from lis_config where name = '" + getPara("device") + "'");

            renderText(re == null ? "{}" : re.getStr("value"));
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
        }
    }

    public void hebing() {
        try {
            String other_uuids[] = getPara("other_uuids").split(",");
            String main_uuid = getPara("main_uuid");
            for (String uuid : other_uuids) {
                Db.update("update lis_testresult set PARENT_UUID='" + main_uuid + "'  where UUID='" + uuid + "'");
            }
            renderText("true");
        } catch (Exception e) {
            renderText("false");
        }
    }

    public void getReagent() {

        renderJson(Db.find("select * from zk_reagent"));
    }

    public void getReagentForDeviceId() {
      
        try {
            renderJson(Db.find("select * from zk_reagent where DEVICE='" + getPara("DEVICEID") + "'"));
        } catch (Exception e) {
            renderText("false");
        }
    }

    public void getPatList() {

        renderJson(Db.find("select * from lis_patient a left join lis_samplecollection b on a.PK=b.INFOPK WHERE date(DATA1)=curdate() group by b.INFOPK "));

    }

    public void queryInfoRpt() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Record info = Db.findFirst("select * from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK where UUID='" + getPara("uuid") + "'");
            map.put("info", info);
            List<Record> results = Db.find("select a.PK,a.RESULTFLAG,a.TESTRESULT,a.TESTID,a.ITEMPK,a.DEVICEID,a.UUID,b.CHINESENAME,a.UNIT,a.TESTSTATUS,a.SAMPLETYPE,a.FEE,"
                    + "a.MEASURETIME,c.MREFLO0,c.MREFHI0,c.FREFLO0,c.FREFHI0,c.Decimalplace,c.DATA1 AS `index`,c.DATA3 AS ranges from lis_testresult a LEFT JOIN lis_testdescribe b on a.ITEMPK=b.PK "
                    + "LEFT JOIN pp_device_test c ON a.TESTID=c.TESTID and a.DEVICEID=c.DEVICEID  "
                    + "where a.UUID='" + getPara("uuid") + "'");
            map.put("results", results);
            Record config = Db.findFirst("select * from lis_config where name = '" + results.get(0).get("DEVICEID") + "'");
            new LisSamplecollection().updateDY(getPara("uuid"));
            if (config != null) {
                map.put("config", config.getStr("value"));
            }
            renderJson(map);
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void autoRpt() {
        render("/view/autoPrint.jsp");
    }

    // 获取签名
    public void getSign() {

        Record re = Db.findFirst("select * from lis_user where name = '" + getPara("name") + "'");
        if (re != null && re.get("sign") != null) {
            renderText(re.get("sign") + "");
        } else {
            renderText("false");
        }

    }
    //保存质控数据
    public void saveZk(){
        String ZK_REAGENT_PK = getPara("ZK_REAGENT_PK");
        String BATCH_NUMBER = getPara("BATCH_NUMBER");
        String sample_date= getPara("sample_date");
        try{
        List<Map<String, String>> results = gson.fromJson(getPara("results"), new TypeToken<List<Map<String, String>>>() {
             }.getType());
        if(results!=null&&!results.isEmpty()){
        
            for(Map<String,String> map:results){
            
                 String sql = "replace into zk_test_result(ZK_REAGENT_PK,BATCH_NUMBER,DEVICEID,TESTID,ZK_DATE,SAVE_USER,CREATETIME,RESULT) values("
                + "?,?,?,?,?,?,?,?)";
                 Db.update(sql,ZK_REAGENT_PK,BATCH_NUMBER,map.get("DEVICEID"),map.get("TESTID"),sample_date,getSessionAttr("name"),DateUtil.getDateEN(),map.get("TESTRESULT"));
                
            }
            Db.update("update lis_testresult set data1='zk' where UUID='"+results.get(0).get("UUID")+"'");
        }
            renderText("true");
        }catch(Exception e){
        e.printStackTrace();
            renderText("false");
        }
              
    }
    // 获取手动设备
    public void getSgDevice(){
    
        renderJson(Db.find("select * from lis_deviceset a left join lis_devicetype b on a.DEVICETYPEPK = b.PK where b.DEVICETYPEDESC='手工' and NOTES is not null "));
        
    }
    

}
